// pages/demo3/demo3.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:[],
    search:"",
    noConnect:false,
    noSearch:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp()
    var that = this

    setTimeout(function () {
      wx.getStorage({
        key: 'userinfo',
        success (res) {
          var userinfo = res.data

          if(userinfo.nickname==""){
            wx.redirectTo({
              url: "/pages/login/login?id="+userinfo.openid
            })
          }
        },
        fail (res){
          console.log(res)
        }
      })
     }, 3000) //延迟时间 这里是1秒
   

     console.log(getApp().globalData.url+"question/");
     

    wx.request({
      url:getApp().globalData.url+"question/",
      data:{
          search:""
      },
      header:{
         "Content-Type":"application/json"
      },
      success:function(res){
        console.log(res)
        that.setData({
          data:res.data.data.reverse(),
          noConnect:false
        })
        wx.setStorage({
          data: res.data.data.reverse(),
          key: 'question_data',
        })
      },
      fail:function(res){
        that.setData({
          noConnect:true
        })
      }
  
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  
  searchInput:function(e){
    var that = this
    that.setData({
      search: e.detail.value
    })
  },
  searchButton:function(){

    var that = this
    if(!that.data.noConnect){
      wx.request({
        url:getApp().globalData.url+"question/",
        data:{
            search:that.data.search
        },
        header:{
          "Content-Type":"application/json"
        },
        success:function(res){
          if (res.data.data.length !=0){
            that.setData({
              data:res.data.data.reverse(),
              noSearch:false
            })
          }else{
            that.setData({
              data:res.data.data,
              noSearch:true
            })
          }
        },
        fail:function(res){
          that.setData({
            data:[],
            noSearch:true
          })
        }
      })
    }
  }
})





